<?php
/**
 * JComments AlphaUserPoints Plugin (for JComments integration with AlphaUserPoints component)
 *
 * @version 3.0
 * @package JComments
 * @author Sergey M. Litvinov (smart@joomlatune.ru)
 * @copyright (C) 2006-2013 by Sergey M. Litvinov (http://www.joomlatune.ru)
 * @license GNU/GPL: http://www.gnu.org/copyleft/gpl.html
 *
 **/
 
class plgJCommentsAlphaUserPoints extends JPlugin
{
        var $api_AUP = '';

	public function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
		$this->api_AUP = JPATH_SITE.DS.'components'.DS.'com_alphauserpoints'.DS.'helper.php';
	}

	function onJCommentsCommentAfterAdd(&$comment) 
	{
		if ($comment->userid) {
			if(file_exists($this->api_AUP)) {
				require_once($this->api_AUP);
				// assign points to comment's author
				AlphaUserPointsHelper::userpoints('plgaup_jcomments_add_comment');
			}
		}
	}

	function onJCommentsCommentAfterDelete(&$comment) 
	{
		if ($comment->userid) {
			if(file_exists($this->api_AUP)) {
				require_once($this->api_AUP);
				$aupid = AlphaUserPointsHelper::getAnyUserReferreID($comment->userid);
				if ($aupid) {
					// assign points to comment's author
					AlphaUserPointsHelper::userpoints('plgaup_jcomments_comment_deleted', $aupid);
				}
			}
		}
	}

	function onJCommentsCommentAfterPublish(&$comment) 
	{
		if(file_exists($this->api_AUP)) {
			require_once($this->api_AUP);
			$aupid = AlphaUserPointsHelper::getAnyUserReferreID($comment->userid);
			if ($aupid) {
				if ($comment->published) {
					// assign points to comment's author
					AlphaUserPointsHelper::userpoints('plgaup_jcomments_comment_published', $aupid);
				} else {
					// assign points to comment's author
					AlphaUserPointsHelper::userpoints('plgaup_jcomments_comment_unpublished', $aupid);
				}
			}
		}
	}

	function onJCommentsCommentAfterVote(&$comment, $value) 
	{
		if(file_exists($this->api_AUP)) {
			require_once($this->api_AUP);
		        $user = & JFactory::getUser();
			if ($user->id) {
				// assign points to user who votes comment
				AlphaUserPointsHelper::userpoints('plgaup_jcomments_vote_comment');
			}

			// assign points to user whose comment has been voted
			$aupid = AlphaUserPointsHelper::getAnyUserReferreID($comment->userid);
			if ($aupid) {
				if ($value > 0) {
					AlphaUserPointsHelper::userpoints('plgaup_jcomments_good_comment', $aupid);
				} else {
					AlphaUserPointsHelper::userpoints('plgaup_jcomments_bad_comment', $aupid);
				}
			}
		}
	}

	function onJCommentsCommentAfterReport(&$comment, &$report) 
	{
		if(file_exists($this->api_AUP)) {
			require_once($this->api_AUP);

		        $user = & JFactory::getUser();
			if ($user->id) {
				// assign points to user who reports comment
				AlphaUserPointsHelper::userpoints('plgaup_jcomments_report_comment');
			}

			// assign points to user whose comment has been voted
			$aupid = AlphaUserPointsHelper::getAnyUserReferreID($comment->userid);
			if ($aupid) {
				AlphaUserPointsHelper::userpoints('plgaup_jcomments_comment_reported', $aupid);
			}
		}
	}
}
?>